<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/6/12
 * Time: 11:48 AM
 * To change this template use File | Settings | File Templates.
 */
class Db_Payments_PaymentMethodsErrorCodes extends Common {
	protected static $tableName = TAB_PAYMENT_METHODS_ERROR_CODES;
	protected static $dbFields = array(
		'id',
		'pmec_payment_method_id',
		'pmec_error_id',
		'pmec_retry_possible',
		'pmec_description',
		'deleted'
	);

	/**
	 * @var Db_Payments_PaymentMethodsErrorCodes
	 */
	public $objectArray;

	public $id;
	public $pmec_payment_method_id;
	public $pmec_error_id;
	public $pmec_retry_possible;
	public $pmec_description;
	public $deleted;

	function __construct($value = null, $fieldName = null) {
		parent::__construct($value, $fieldName);
	}

	public static function getAllPaymentMethodsErrorCodes($limit, $sqlSearch = '', $orderBy = '') {
		$joinSearch = null;
		if (empty($sqlSearch)) {
			$sqlSearch = '1=1';
			$joinSearch = '';
		} else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pm.pm_module AND tr.t_key = pm.pm_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlSearch .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}
		$sql = "SELECT *, `pmec`.`id` AS `pmec_id` FROM ?f AS `pmec`
				INNER JOIN ?f AS `pm` ON `pm`.`id` = `pmec`.`pmec_payment_method_id`
				{$joinSearch}
				WHERE `pm`.`deleted` = 0 AND `pmec`.`deleted` = 0 AND {$sqlSearch} {$orderBy} {$limit}";

		return self::$DB->select($sql, self::$tableName, TAB_PAYMENT_METHODS);
	}

	public static function getTotalPaymentMethodsErrorCodes($sqlCondition = '1=1') {
		if (empty($sqlCondition)) {
			$sqlCondition = '1=1';
			$joinSearch = '';
		} else {
			$joinSearch = " LEFT JOIN " . TAB_TRANSLATIONS . " tr ON tr.t_module = pm.pm_module AND tr.t_key = pm.pm_name AND tr.t_lang_id = " . conf('COUNTRY_LANG_ID') ;

			$sqlCondition .= " OR (`tr`.`t_content` LIKE " . self::$DB->escapeValue('%' . str_replace('%', "\\%", post('sSearch')) . '%') . ")";
		}
		$sql = "SELECT COUNT(*) FROM ?f AS `pmec`
				INNER JOIN ?f AS `pm` ON `pm`.`id` = `pmec`.`pmec_payment_method_id`
				{$joinSearch}
				WHERE `pm`.`deleted` = 0 AND `pmec`.`deleted` = 0 AND {$sqlCondition} ;";

		return self::$DB->selectCell($sql, self::$tableName, TAB_PAYMENT_METHODS);
	}


}
